package hot100;

import DTO.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class rightSideView {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        depth(root, 0, ret);
        return ret;
    }

    public void depth(TreeNode node, int currentDepth, List<Integer> ret) {
        if (node == null) {
            return;
        }

        // 关键逻辑：当当前深度等于结果集大小时，说明是第一次访问该深度
        if (currentDepth == ret.size()) {
            ret.add(node.val);
        }

        // 优先递归右子树
        depth(node.right, currentDepth + 1, ret);
        depth(node.left, currentDepth + 1, ret);
    }
}
